package com.mrdeer.web.controller.sys;

import cn.hutool.core.util.StrUtil;
import com.google.gson.Gson;
import com.mrdeer.common.JSONResult;
import com.mrdeer.common.constants.SettingConstant;
import com.mrdeer.common.entity.dto.*;
import com.mrdeer.common.entity.sys.Setting;
import com.mrdeer.utils.sms.SmsUtil;
import com.mrdeer.web.service.sys.SysSettingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/mrdeer/setting")
@Api(tags = "基本配置接口")
public class SysSettingController {

    @Resource
    private SysSettingService sysSettingService;

    @ApiOperation(value = "查看私密配置")
    @RequestMapping(value = "/seeSecret/{settingName}", method = RequestMethod.GET)
    public JSONResult seeSecret(@PathVariable String settingName) {
        String result = "";
        Setting setting = sysSettingService.getById(settingName);
        if (setting == null || StrUtil.isBlank(setting.getValue())) {
            return JSONResult.errorMsg("配置不存在");
        }
        if (settingName.equals(SettingConstant.QINIU_OSS) || settingName.equals(SettingConstant.ALI_OSS) || settingName.equals(SettingConstant.TENCENT_OSS) || settingName.equals(SettingConstant.MINIO_OSS)) {
            result = new Gson().fromJson(setting.getValue(), OssSetting.class).getSecretKey();
        } else if (settingName.equals(SettingConstant.ALI_SMS) || settingName.equals(SettingConstant.TENCENT_SMS)) {
            result = new Gson().fromJson(setting.getValue(), SmsSetting.class).getSecretKey();
        } else if (settingName.equals(SettingConstant.EMAIL_SETTING)) {
            result = new Gson().fromJson(setting.getValue(), EmailSetting.class).getPassword();
        } else if (settingName.equals(SettingConstant.VAPTCHA_SETTING)) {
            result = new Gson().fromJson(setting.getValue(), VaptchaSetting.class).getSecretKey();
        }
        return JSONResult.ok(result);
    }

    @ApiOperation(value = "检查OSS配置")
    @RequestMapping(value = "/oss/check", method = RequestMethod.GET)
    public JSONResult ossCheck() {
        Setting setting = sysSettingService.get(SettingConstant.OSS_USED);
        if (setting == null || StrUtil.isBlank(setting.getValue())) {
            return JSONResult.errorMsg("您还未配置第三方OSS服务");
        }
        return JSONResult.ok(setting.getValue());
    }

    @ApiOperation(value = "检查短信配置")
    @RequestMapping(value = "/sms/check", method = RequestMethod.GET)
    public JSONResult smsCheck() {
        Setting setting = sysSettingService.get(SettingConstant.SMS_USED);
        if (setting == null) {
            return JSONResult.errorMsg("您还未配置短信服务");
        }
        return JSONResult.ok(setting.getValue());
    }

    @RequestMapping(value = "/oss/{serviceName}", method = RequestMethod.GET)
    @ApiOperation(value = "查看OSS配置")
    public JSONResult oss(@PathVariable String serviceName) {

        Setting setting = null;
        if (serviceName.equals(SettingConstant.QINIU_OSS)
                || serviceName.equals(SettingConstant.ALI_OSS)
                || serviceName.equals(SettingConstant.TENCENT_OSS)
                || serviceName.equals(SettingConstant.MINIO_OSS)
                || serviceName.equals(SettingConstant.LOCAL_OSS)) {
            setting = sysSettingService.get(serviceName);
        }
        if (setting == null || StrUtil.isBlank(setting.getValue())) {
            setting = new Setting();
            setting.setId(serviceName);
            sysSettingService.saveOrUpdate(setting);
            return JSONResult.ok(null);
        }
        OssSetting ossSetting = new Gson().fromJson(setting.getValue(), OssSetting.class);
        ossSetting.setSecretKey("*********");
        return JSONResult.ok(ossSetting);
    }

    @RequestMapping(value = "/oss/set", method = RequestMethod.POST)
    @ApiOperation(value = "OSS配置")
    public JSONResult ossSet(@RequestBody OssSetting ossSetting) {
        String name = ossSetting.getServiceName();
        Setting setting = sysSettingService.get(name);
        if (name.equals(SettingConstant.QINIU_OSS) || name.equals(SettingConstant.ALI_OSS) || name.equals(SettingConstant.TENCENT_OSS)
                || name.equals(SettingConstant.MINIO_OSS)) {
            //判断是否修改secrectKey保留原secrectKey 避免保存***加密字符
            if (StrUtil.isNotBlank(setting.getValue()) && !ossSetting.getChanged()) {
                String secrectKey = new Gson().fromJson(setting.getValue(), OssSetting.class).getSecretKey();
                ossSetting.setSecretKey(secrectKey);
            }
        }
        setting.setValue(new Gson().toJson(ossSetting));
        sysSettingService.saveOrUpdate(setting);
        //保存启用的OSS服务商
        Setting used = sysSettingService.get(SettingConstant.OSS_USED);
        if (used == null) {
            used = new Setting();
        }
        used.setId(SettingConstant.OSS_USED);
        used.setValue(name);
        sysSettingService.saveOrUpdate(used);
        return JSONResult.ok(null);
    }


    @RequestMapping(value = "/sms/templateCode/{serviceName}/{type}", method = RequestMethod.GET)
    @ApiOperation(value = "查看短信模板配置")
    public JSONResult smsTemplateCode(@PathVariable String serviceName, @PathVariable Integer type) {
        String templateCode = "";
        if (type != null) {
            String id = serviceName + "_" + SmsUtil.getTemplateSuffix(type);
            Setting setting = sysSettingService.get(id);
            if (setting == null) {
                setting = new Setting();
                setting.setId(id);
                sysSettingService.saveOrUpdate(setting);
            }
            if (StrUtil.isNotBlank(setting.getValue())) {
                templateCode = setting.getValue();
            }
        }
        return JSONResult.ok(templateCode);
    }

    @RequestMapping(value = "/sms/{serviceName}", method = RequestMethod.GET)
    @ApiOperation(value = "查看短信配置")
    public JSONResult sms(@PathVariable String serviceName) {
        Setting setting = new Setting();
        if (serviceName.equals(SettingConstant.ALI_SMS) || serviceName.equals(SettingConstant.TENCENT_SMS)) {
            setting = sysSettingService.get(serviceName);
        }
        if (setting == null || StrUtil.isBlank(setting.getId())) {
            setting = new Setting();
            setting.setId(serviceName);
            sysSettingService.saveOrUpdate(setting);
            return JSONResult.ok(null);
        }
        SmsSetting smsSetting = new Gson().fromJson(setting.getValue(), SmsSetting.class);
        smsSetting.setSecretKey("*********");
        return JSONResult.ok(smsSetting);
    }

    @RequestMapping(value = "/sms/set", method = RequestMethod.POST)
    @ApiOperation(value = "短信配置")
    public JSONResult smsSet(@RequestBody SmsSetting smsSetting) {
        Setting setting = sysSettingService.get(smsSetting.getServiceName());
        if (setting == null) {
            setting = new Setting();
            setting.setId(smsSetting.getServiceName());
            sysSettingService.saveOrUpdate(setting);
        }
        setting.setValue(new Gson().toJson(smsSetting));
        sysSettingService.saveOrUpdate(setting);

        Setting used = sysSettingService.get(SettingConstant.SMS_USED);
        if (null == used) {
            used = new Setting();
        }
        used.setId(SettingConstant.SMS_USED);
        used.setValue(smsSetting.getServiceName());
        sysSettingService.saveOrUpdate(used);
        return JSONResult.ok(setting);
    }

    @RequestMapping(value = "/email", method = RequestMethod.GET)
    @ApiOperation(value = "查看Email配置")
    public JSONResult email() {
        Setting setting = sysSettingService.get(SettingConstant.EMAIL_SETTING);
        if (setting == null || StrUtil.isBlank(setting.getId())) {
            setting = new Setting();
            setting.setId(SettingConstant.EMAIL_SETTING);
            sysSettingService.saveOrUpdate(setting);
            return JSONResult.ok(null);
        }
        EmailSetting emailSetting = new Gson().fromJson(setting.getValue(), EmailSetting.class);
        return JSONResult.ok(emailSetting);
    }

    @RequestMapping(value = "/email/set", method = RequestMethod.POST)
    @ApiOperation(value = "email配置")
    public JSONResult emailSet(@RequestBody EmailSetting emailSetting) {
        Setting setting = sysSettingService.get(SettingConstant.EMAIL_SETTING);
        setting.setValue(new Gson().toJson(emailSetting));
        sysSettingService.saveOrUpdate(setting);
        return JSONResult.ok(setting);
    }

    @RequestMapping(value = "/vaptcha", method = RequestMethod.GET)
    @ApiOperation(value = "查看Vaptcha配置")
    public JSONResult vaptcha() {
        Setting setting = sysSettingService.get(SettingConstant.VAPTCHA_SETTING);
        if (setting == null || StrUtil.isBlank(setting.getId())) {
            setting = new Setting();
            setting.setId(SettingConstant.VAPTCHA_SETTING);
            sysSettingService.saveOrUpdate(setting);
            return JSONResult.ok(null);
        }
        VaptchaSetting vaptchaSetting = new Gson().fromJson(setting.getValue(), VaptchaSetting.class);
        return JSONResult.ok(vaptchaSetting);
    }

    @RequestMapping(value = "/vaptcha/set", method = RequestMethod.POST)
    @ApiOperation(value = "vaptcha配置")
    public JSONResult vaptchaSet(@RequestBody VaptchaSetting vaptchaSetting) {
        Setting setting = sysSettingService.get(SettingConstant.VAPTCHA_SETTING);
        setting.setValue(new Gson().toJson(vaptchaSetting));
        sysSettingService.saveOrUpdate(setting);
        return JSONResult.ok(setting);
    }

    @RequestMapping(value = "/other", method = RequestMethod.GET)
    @ApiOperation(value = "查看其它配置")
    public JSONResult other() {
        Setting setting = sysSettingService.get(SettingConstant.OTHER_SETTING);
        if (setting == null || StrUtil.isBlank(setting.getId())) {
            setting = new Setting();
            setting.setId(SettingConstant.OTHER_SETTING);
            sysSettingService.saveOrUpdate(setting);
            return JSONResult.ok(null);
        }
        OtherSetting otherSetting = new Gson().fromJson(setting.getValue(), OtherSetting.class);
        return JSONResult.ok(otherSetting);
    }

    @RequestMapping(value = "/other/set", method = RequestMethod.POST)
    @ApiOperation(value = "其它配置")
    public JSONResult otherSet(@RequestBody OtherSetting otherSetting) {
        Setting setting = sysSettingService.get(SettingConstant.OTHER_SETTING);
        setting.setValue(new Gson().toJson(otherSetting));
        sysSettingService.saveOrUpdate(setting);
        return JSONResult.ok(setting);
    }


    @RequestMapping(value = "/notice", method = RequestMethod.GET)
    @ApiOperation(value = "查看公告配置")
    public JSONResult notice() {
        Setting setting = sysSettingService.get(SettingConstant.NOTICE_SETTING);
        if (setting == null || StrUtil.isBlank(setting.getId())) {
            setting = new Setting();
            setting.setId(SettingConstant.NOTICE_SETTING);
            sysSettingService.saveOrUpdate(setting);
            return JSONResult.ok(null);
        }
        NoticeSetting noticeSetting = new Gson().fromJson(setting.getValue(), NoticeSetting.class);
        return JSONResult.ok(noticeSetting);
    }

    @RequestMapping(value = "/notice/set", method = RequestMethod.POST)
    @ApiOperation(value = "其它配置")
    public JSONResult noticeSet(@RequestBody NoticeSetting noticeSetting) {
        Setting setting = sysSettingService.get(SettingConstant.NOTICE_SETTING);
        setting.setValue(new Gson().toJson(noticeSetting));
        sysSettingService.saveOrUpdate(setting);
        return JSONResult.ok(setting);
    }
}
